<?php
// +----------------------------------------------------------------------
// | sfadmin - Suifeng Administration
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2020 http://suifeng.tech All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: suifeng <1787585212@qq.com>
// +----------------------------------------------------------------------

namespace app\wechat\service;

use suifeng\wechat\WechatSDK;
use suifeng\wechat\TPWechat;
use app\admin\common\AdminException;

final class Mp
{
    /**
     * 获取公众号类型列表。
     *
     * @return string[]
     */
    public function get_mp_type_list()
    {
        return array(
            1 => '普通订阅号',
            2 => '认证订阅号',
            3 => '普通服务号',
            4 => '认证服务号',
        );
    }

    /**
     * 获取管理的公众号列表。
     *
     * @return string[]
     */
    public function get_mp_list()
    {
        $list = cache('service_wechat_mp::get_mp_list');

        if (!$list || APP_DEBUG) {
            $list = model('wechat/mp')->order('id asc')->select();

            foreach ($list as $i => $row) {
                //$row['content'] = helper('admin/json')->decode($row['content']);
                $list[$i] = $row->getData();
            }

            cache('service_wechat_mp::get_mp_list', $list);
        }

        return $list;
    }

    /**
     * 获取管理的公众号信息。
     */
    public function get_mp_info($id_or_number)
    {
        foreach ($this->get_mp_list() as $mp_info) {
            if ($mp_info['id'] == $id_or_number || $mp_info['mp_number'] == $id_or_number || $mp_info['origin_id'] == $id_or_number) {
                return $mp_info;
            }
        }
        return null;
    }

    /**
     * 获取默认的公众号。
     *
     * @return string
     */
    public function get_default_mp_number()
    {
        $list = $this->get_mp_list();
        if ($list) {
            return $list[0]['mp_number'];
        } else {
            return null;
        }
    }

    /**
     * 清除缓存的公众号信息。
     */
    public function clear_cache()
    {
        cache('service_wechat_mp::get_mp_list', null);
    }

    /**
     * 获取wechat调用对象。
     *
     * @param unknown $id_or_number
     * @return TPWechat
     */
    public function get_wx_object($id_or_number)
    {
        static $instance_list = array();

        $mp_info = $this->get_mp_info($id_or_number);
        if (!$mp_info) {
            throw new AdminException('无法识别的公众号标识: ' . $id_or_number);
        }

        $mp_number = $mp_info['mp_number'];

        if (isset($instance_list[$mp_number])) {
            return $instance_list[$mp_number];
        }

        $instance_list[$mp_number] = new TPWechat(array(
            'appid' => $mp_info['appid'],
            'appsecret' => $mp_info['appsecret'],
            'encodingaeskey' => $mp_info['encodingaeskey'],
            'token' => $mp_info['token'],
        ));

        return $instance_list[$mp_number];
    }
}
